package leetcode.code0486;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public boolean PredictTheWinner(int[] nums) {
		int len = nums.length;
		Integer[][][] dp = new Integer[len][len][2];
		int player = this.find(nums, 0, nums.length - 1, 0, dp);
		return player >= 0 ? true : false;
	}

	private int find(int[] nums, int l, int r, int player, Integer[][][] dp) {
		if (l == r) {
			return player == 0 ? nums[l] : -nums[l];
		}
		if (dp[l][r][player] != null) {
			return dp[l][r][player];
		}
		int next = player == 0 ? 1 : 0;
		int ans1 = this.find(nums, l + 1, r, next, dp);
		int ans2 = this.find(nums, l, r - 1, next, dp);
		int ans = 0;
		if (player == 0) {
			ans1 += nums[l];
			ans2 += nums[r];
			ans = Math.max(ans1, ans2);
		} else {
			ans1 -= nums[l];
			ans2 -= nums[r];
			ans = Math.min(ans1, ans2);
		}
		dp[l][r][player] = ans;
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(false, this.PredictTheWinner(HelpDebug.str2array3("[1,5,2]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(true, this.PredictTheWinner(HelpDebug.str2array3("[1,5,233,7]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
